/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

/**
 *
 * @author Vegard
 */

import java.sql.*;
import Module.*;


public class ActionListController {

    private Database database = new Database();

    public boolean registerActionList(ActionList actionList){

        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            String[] autogen = {"id"};
            PreparedStatement statement = conn.prepareStatement(
                    "Insert into actionList values(DEFAULT,?,?,?,?)",autogen);
            statement.setInt(1, actionList.getProjectId());
            statement.setString(2, actionList.getDescription());
            statement.setString(3, actionList.getStatus());
            statement.setString(4, actionList.getResponsible());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch (Exception e) {
            System.err.println(e);

        }
        return false;
    }
    public boolean editActionList(ActionList actionList){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("Update actionList "
                    + "set projectId=?, description=?, status=?, responsible=? where projectId =?");
            statement.setInt(1, actionList.getProjectId());
            statement.setString(2, actionList.getDescription());
            statement.setString(3, actionList.getStatus());
            statement.setString(4, actionList.getDescription());
            statement.setInt(5, actionList.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }

        return false;
    }
    public boolean deleteActionList(ActionList actionList){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from actionList where projectId = ?");
            statement.setInt(1, actionList.getProjectId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }
        return false;
    }

    public ActionList getActionlist(Project project){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from actionList where projectId =?");
            statement.setInt(1, project.getId());
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                return new ActionList(rs.getInt(1),rs.getInt(2), rs.getString(3),rs.getString(4), rs.getString(5));
            }


        }catch(Exception e){
            System.err.println(e);
        }
        return null;
    }
}
